package com.xchen.imweb.auth;

import com.xchen.imweb.message.WebSocketSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    @PostMapping("")
    @ResponseBody
    private Result<Boolean> login(HttpServletRequest request, String username) {
        boolean usernameExist = WebSocketSession.isUsernameExist(username);
        if(usernameExist) {
            return Result.error(username + "已存在，请更换一个名字");
        }
        HttpSession session = request.getSession();
        session.setAttribute("user", username);
        return Result.ok(true);
    }

    @GetMapping("")
    public String login() {
        return "login";
    }

}
